package com.jike.jkgame;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

import com.jike.jkgame.data.ResourceType;
import com.viewpagerindicator.CirclePageIndicator;

public class StrengthenFragment extends Fragment implements OnClickListener {

	private PagerAdapter mAdapter;
	private ViewPager mPager;
	private CirclePageIndicator mIndicator;
	private View mStrengthResultView;
	private LayoutInflater mInflater;
	private Button mSelectAllBtn;
	private Button mActionBtn;

	private List<GridView> mPages = new ArrayList<GridView>();

	private OnItemClickListener mItemClickListner = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			View checkView = view.findViewById(R.id.iv_check);
			boolean visible = checkView.getVisibility() == View.VISIBLE;
			checkView.setVisibility(visible ? View.GONE : View.VISIBLE);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mInflater = LayoutInflater.from(getActivity());

		initPages();
		mAdapter = new ItemPagerAdapter();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.strength_layout, container, false);

		mPager = (ViewPager) v.findViewById(R.id.pager);
		mPager.setAdapter(mAdapter);

		mIndicator = (CirclePageIndicator) v.findViewById(R.id.indicator);
		mIndicator.setViewPager(mPager);
		
		mSelectAllBtn = (Button) v.findViewById(R.id.btn_selectall);
		mSelectAllBtn.setOnClickListener(this);
		
		mActionBtn = (Button) v.findViewById(R.id.btn_strengthen);
		mActionBtn.setOnClickListener(this);
		
		mStrengthResultView = v.findViewById(R.id.ll_strength_result);
		v.findViewById(R.id.btn_general).setOnClickListener(this);
		v.findViewById(R.id.btn_weapon).setOnClickListener(this);
		v.findViewById(R.id.btn_armor).setOnClickListener(this);
		v.findViewById(R.id.btn_mount).setOnClickListener(this);
		v.findViewById(R.id.btn_treasure).setOnClickListener(this);
		mStrengthResultView.findViewById(R.id.btn_strength_result_close).setOnClickListener(this);
		return v;
	}

	@Override
	public void onDestroyView() {
		mPager.removeAllViews();
		super.onDestroyView();
	}

	private void initPages() {
		mPages.clear();
		for (int i = 0; i < 5; ++i) {
			GridView view = (GridView) mInflater.inflate(
					R.layout.items_pager_layout, null, false);
			view.setAdapter(new ItemsAdapter());
			view.setOnItemClickListener(mItemClickListner);
			mPages.add(view);
		}
	}

	class ItemsAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return 12;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;

			if (view == null) {
				view = mInflater.inflate(R.layout.strength_item_layout, parent,
						false);
			}

			return view;
		}

	}
	
	private void gotoSelectResource(ResourceType type) {
		Intent intent = new Intent(getActivity(), SelectResourcesActicity.class);
		intent.putExtra("res_type", type);

		startActivityForResult(intent, 100 + type.ordinal());
	} 

	class ItemPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return mPages.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((ViewPager) container).removeView((GridView) object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			((ViewPager) container).addView(mPages.get(position));

			return mPages.get(position);
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_selectall:
			// TODO 选择当前page的所有Item
			break;
		case R.id.btn_strengthen:
			// TODO 进行强化
			mStrengthResultView.setVisibility(View.VISIBLE);
			break;
		case R.id.btn_general:
			gotoSelectResource(ResourceType.General);
			break;
		case R.id.btn_weapon:
			gotoSelectResource(ResourceType.Weapon);
			break;
		case R.id.btn_armor:
			gotoSelectResource(ResourceType.Armor);
			break;
		case R.id.btn_mount:
			gotoSelectResource(ResourceType.Mount);
			break;
		case R.id.btn_treasure:
			gotoSelectResource(ResourceType.Treasure);
			break;
		case	R.id.btn_strength_result_close:
			mStrengthResultView.setVisibility(View.GONE);
			break;
		default:
			break;
		}
		
	}

}
